//
//  FPMyInfoController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/24.
//

#import "FPMyInfoController.h"
#import "FPAuthenticationController.h"

@interface FPMyInfoController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLab;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UILabel *tipLab1;
@property (weak, nonatomic) IBOutlet UILabel *tipLab2;
@property (weak, nonatomic) IBOutlet UILabel *tipLab3;
@property (weak, nonatomic) IBOutlet UILabel *tipLab4;
@property (weak, nonatomic) IBOutlet UILabel *tipLab5;
@property (weak, nonatomic) IBOutlet UIView *lineView1;
@property (weak, nonatomic) IBOutlet UIView *lineView2;
@property (weak, nonatomic) IBOutlet UILabel *tipLab6;
@property (weak, nonatomic) IBOutlet UILabel *tipLab7;

@property (weak, nonatomic) IBOutlet UIButton *changeIDBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnToBottom;


@end

@implementation FPMyInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUI) name:FPAuthenticationSuccessNotification object:nil];
}

- (void)refreshUI {
    
    
}

- (void)initUI {
    
    [self setTitle:@"我的资料" style:(XYCustomNavigationStyleClear) hiddenBackBtn:NO];
    self.view.backgroundColor = [UIColor hexColor:@"#F4F6FA"];
    
    self.lineView1.backgroundColor = [UIColor hexColor:@"#DEDEDE"];
    self.lineView2.backgroundColor = [UIColor hexColor:@"#DEDEDE"];
    
    self.changeIDBtn.clipsToBounds = YES;
    self.changeIDBtn.layer.cornerRadius = 22.f;
    self.changeIDBtn.backgroundColor = MAIN_BLUE_COLOR;
    self.btnToBottom.constant = -20.f - UIDevice.safeDistanceBottom;
    
    self.backView.backgroundColor = UIColor.whiteColor;
    self.backView.clipsToBounds = YES;
    self.backView.layer.cornerRadius = 8.f;
    self.tipLab1.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab2.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab3.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab4.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab5.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab6.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab7.textColor = [UIColor hexColor:@"#3A3A3A"];
    
    self.titleLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.subTitleLab.textColor = [UIColor hexColor:@"#6A6A6A"];
    
    self.viewHeight.constant = 283.f - 92.f + 92.f / (139.f * 2.f + 16.f) * SCREEN_WIDTH - 20.f; // 20.f: 计算上因为两个图片中间有一定间隔 会导致整体高度偏高
    
    
    NSString *text1 = @"认证获取更高额度";
    NSString *text2 = @"仅用于身份实名核验、信用审核及额度评估，信息将严格保密。";
    
    NSRange range1 = [text1 rangeOfString:@"更高额度"];
    NSRange range2 = [text2 rangeOfString:@"信息将严格保密"];
    
    NSMutableAttributedString *attStr1 = [[NSMutableAttributedString alloc] initWithString:text1];
    [attStr1 addAttribute:NSForegroundColorAttributeName value:[UIColor hexColor:@"#0D6FFF"] range:range1];
    self.titleLab.attributedText = attStr1;
    
    NSMutableAttributedString *attStr2 = [[NSMutableAttributedString alloc] initWithString:text2];
    [attStr2 addAttribute:NSFontAttributeName value:BFONT(12.f) range:range2];
    [attStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor hexColor:@"#3A3A3A"] range:range2];
    self.subTitleLab.attributedText = attStr2;
    
    
    FPUserInfoModel *userInfo = FPUserInfoModel.sharedUserInfo;
    
    self.tipLab4.text = [self formatUserName:userInfo.idcardName];
    
    self.tipLab5.text = [userInfo.idcardNumber stringByReplacingCharactersInRange:NSMakeRange(2, userInfo.idcardNumber.length - 4) withString:@"*************"];
}

#pragma mark - 用户名脱敏
- (NSString *)formatUserName:(NSString *)userName {
    
    // 获取用户名的长度
    NSUInteger length = userName.length;
    
    // 如果用户名长度为0，返回空字符串
    if (length == 0) {
        return @"";
    }
    
    // 如果只有一个字
    if (length == 1) {
        return userName; // 返回原名
    }
    
    // 如果用户名有两个字，第二个字用*代替
    if (length == 2) {
        return [userName stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:@"*"];
    }
    
    // 如果有多个字，显示第一个字和最后一个字，中间部分用*代替
    NSString *firstCharacter = [userName substringToIndex:1];
    NSString *lastCharacter = [userName substringFromIndex:length - 1];
    NSString *stars = [@"" stringByPaddingToLength:length - 2 withString:@"*" startingAtIndex:0];
    
    return [firstCharacter stringByAppendingString:[stars stringByAppendingString:lastCharacter]];
}


- (IBAction)changeIdentifierCardAction:(UIButton *)sender {
    
    FPAuthenticationController *vc = [[FPAuthenticationController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
